/**
 ******************************************************************************
 * @file    rtc.h
 * @author  hyseim software Team
 * @date    15-Mar-2024
 * @brief   This file provides all the headers of the rtc functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RTC_H__
#define __RTC_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "chip_define.h"
#include "utils.h"

/* ================================================================================ */
/* ================              Real-Time Clock (RTC)             ================ */
/* ================================================================================ */
typedef struct
{
    __I  uint32_t CCVR;             /*!< Current Counter Value Register,             Address offset: 0x000 */
    __IO uint32_t CMR;              /*!< Counter Match Register,                     Address offset: 0x004 */
    __IO uint32_t CLR;              /*!< Counter Load Register,                      Address offset: 0x008 */
    __IO uint32_t CCR;              /*!< Counter Control Register,                   Address offset: 0x00C */
    __I  uint32_t STAT;             /*!< Interrupt Status Register,                  Address offset: 0x010 */
    __I  uint32_t RSTAT;            /*!< Interrupt Raw Status Register,              Address offset: 0x014 */
    __I  uint32_t EOI;              /*!< End of Interrupt Register,                  Address offset: 0x018 */
    __I  uint32_t COMP_VERSION;     /*!< Component Version Register,                 Address offset: 0x01C */
    __IO uint32_t CPSR;             /*!< Counter Prescaler Register,                 Address offset: 0x020 */
    __I  uint32_t CPCVR;            /*!< Current Prescaler Counter Value Register,   Address offset: 0x024 */
} RTC_t;

#define RTC              ((RTC_t *) RTC_BASE)
/* ================================================================================ */
/* ================              Real-Time Clock (RTC)             ================ */
/* ================================================================================ */
/********************************  Bit definition for RTC_CCR register  ********************************/
#define RTC_CCR_INTR_EN           (0x1U << 0)        /*!< RTC interrupt enable */
#define RTC_CCR_INTR_MASK         (0x1U << 1)        /*!< RTC interrupt mask configure */
#define RTC_CCR_RTC_EN            (0x1U << 2)        /*!< RTC counter start */
#define RTC_CCR_WRAP_EN           (0x1U << 3)        /*!< RTC wrap counter mode */
#define RTC_CCR_PSCLR_EN          (0x1U << 4)        /*!< RTC Prescaler configure */

/********************************  Bit definition for RTC_RSTAT register  ********************************/
#define RTC_RSTAT_RSTAT           (0x1U << 0)        /*!< RTC interrupt status */

/** @addtogroup IM110GW_RTC_Driver
  * @{
  */

/** @addtogroup RTC
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** 
  * @brief  RTC Init Structure definition  
  */
typedef struct
{
    uint8_t RTC_IntrEn;      /*!< Interrupt control of RTC 
                                  This parameter can be a value of @ref RTC_InterruptGeneration */

    uint8_t RTC_IntrMask;    /*!< Interrupt Mask Control of RTC 
                                  This parameter can be a value of @ref RTC_InterruptMask */

    uint8_t RTC_WrapEn;      /*!< RTC WRAP function control 
                                  This parameter can be a value of @ref RTC_WrapMode */

    uint16_t RTC_PsclrEn;    /*!< RTC Prescaler control 
                                  This parameter can be a value of @ref RTC_PrescalerMode */
} RTC_Init_t;

/* Exported constants --------------------------------------------------------*/

/** @defgroup RTC_Exported_Constants
  * @{
  */

/** @defgroup RTC_InterruptGeneration
  * @{
  */
#define RTC_Interrupt_Enable            RTC_CCR_INTR_EN
#define RTC_Interrupt_Disable           (0x0 << 0)
/**
  * @}
  */


/** @defgroup RTC_InterruptMask 
  * @{
  */
#define RTC_InterruptMask_Enable        RTC_CCR_INTR_MASK
#define RTC_InterruptMask_Disable       (0x0 << 1)
/**
  * @}
  */


/** @defgroup RTC_WrapMode
  * @{
  */
#define RTC_Wrap_Enable                 RTC_CCR_WRAP_EN
#define RTC_Wrap_Disable                (0x0 << 3)
/**
  * @}
  */


/** @defgroup RTC_PrescalerMode
  * @{
  */
#define RTC_Prescaler_Enable            RTC_CCR_PSCLR_EN
#define RTC_Prescaler_Disable           (0x0 << 4)
/**
  * @}
  */

/**
  * @}
  */

/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

void RTC_Enable(void);
void RTC_Disable(void);
void RTC_SetCounterMatch(uint32_t MatchValue);
void RTC_SetCounterLoad(uint32_t LoadValue);
void RTC_SetCounterPrescaler(uint32_t PrescalerValue);
void RTC_Init(RTC_Init_t *RTC_InitStruct, uint32_t LoadValue, uint32_t MatchValue, uint32_t PrescalerValue);
void RTC_StructInit(RTC_Init_t *RTC_StructInit);
uint32_t RTC_GetCounter(void);
FlagStatus_t RTC_GetITStatus(void);
void RTC_ClearFlag(void);

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif
